package com.zxg.zbc.dal.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.zxg.util.CommonUtil;
import com.zxg.zbc.domain.UIResult;

/**
 * 基础查询类
 * 
 * @author HangDing
 *
 *         2014年1月1日
 */
public class CommonQuery<T> extends BasePagerQuery {

  /**
   * 
   */
  private static final long    serialVersionUID = 1L;
  private String               orderByField;
  private String               order            = "asc";
  private Set<String>          selectFields     = new HashSet<String>();
  private Map<String, Object>  params;
  private Map<String, Integer> paramsAnd;
  private Map<String, Object>  paramsStart;
  private Map<String, Object>  paramsEnd;
  private Map<String, Integer> paramsAndNot;
  private Map<String, String>  paramsLike;

  public CommonQuery() {
    params = new HashMap<String, Object>();
    paramsAnd = new HashMap<String, Integer>();
    paramsAndNot = new HashMap<String, Integer>();
    paramsLike = new HashMap<String, String>();

    paramsStart = new HashMap<String, Object>();
    paramsEnd = new HashMap<String, Object>();
  }

  private List<T> results = new ArrayList<T>();
  private String  htmlContent;

  private Long    userId;

  public List<T> getResults() {
    return results;
  }

  public void setResults(List<T> results) {
    this.results = results;
  }

  public Long getUserId() {
    return userId;
  }

  public void setUserId(Long userId) {
    this.userId = userId;
  }

  public Map<String, Object> getParams() {
    params.put("startRow", this.getStartRow());
    params.put("pageSize", this.getPageSize());
    if (this.userId != null)
      params.put("userId", this.userId);
    return params;
  }

  public Map<String, Object> getParamsExceptPageParam() {

    return params;
  }

  public void setParams(Map<String, Object> params) {
    this.params = params;
  }

  public void addParams(String key, Object value) {
    this.params.put(key, value);
  }

  public UIResult convertSuccessJsonResult() {
    UIResult res = new UIResult(true, this.results, "success");
    return res;
  }

  public String getOrderByField() {
    return orderByField;
  }

  public void setOrderByField(String orderByField) {
    this.orderByField = orderByField;
  }

  public String getOrder() {
    return order;
  }

  public void setOrder(String order) {
    this.order = order;
  }

  public void setDescOrder() {
    this.order = "desc";
  }

  public Set<String> getSelectFields() {
    return selectFields;
  }

  public String getSelectSql() {
    if (selectFields == null || selectFields.size() == 0)
      return " * ";

    return " " + CommonUtil.join(selectFields.iterator(), ',');
  }

  public void setSelectFields(Set<String> selectFields) {
    this.selectFields = selectFields;
  }

  public void addSelectFiled(String field) {
    this.selectFields.add(field);
  }

  public Map<String, Integer> getParamsAnd() {
    return paramsAnd;
  }

  public void setParamsAnd(Map<String, Integer> paramsAnd) {
    this.paramsAnd = paramsAnd;
  }

  public void addParamsAnd(String field, Integer value) {
    this.paramsAnd.put(field, value);
  }

  public Map<String, String> getParamsLike() {
    return paramsLike;
  }

  public void setParamsLike(Map<String, String> paramsLike) {
    this.paramsLike = paramsLike;
  }

  public void addParamsLike(String field, String value) {
    this.paramsLike.put(field, value);
  }

  public Map<String, Integer> getParamsAndNot() {
    return paramsAndNot;
  }

  public void setParamsAndNot(Map<String, Integer> paramsAndNot) {
    this.paramsAndNot = paramsAndNot;
  }

  public void addParamsAndNot(String field, Integer value) {
    this.paramsAndNot.put(field, value);
  }

  public String getHtmlContent() {
    return htmlContent;
  }

  public void setHtmlContent(String htmlContent) {
    this.htmlContent = htmlContent;
  }

  public Map<String, Object> getParamsStart() {
    return paramsStart;
  }

  public Map<String, Object> getParamsEnd() {
    return paramsEnd;
  }

  public void setParamsEnd(Map<String, Object> paramsEnd) {
    this.paramsEnd = paramsEnd;
  }

  public void setParamsStart(Map<String, Object> paramsStart) {
    this.paramsStart = paramsStart;
  }

  public void addParamsStart(String field, Object value) {
    this.paramsStart.put(field, value);
  }

  public void addParamsEnd(String field, Object value) {
    this.paramsEnd.put(field, value);
  }

  @Override
  public String toString() {
    return "CommonQuery [orderByField=" + orderByField + ", order=" + order + ", selectFields=" + selectFields + ", params=" + params + ", paramsAnd="
        + paramsAnd + ", paramsStart=" + paramsStart + ", paramsEnd=" + paramsEnd + ", paramsAndNot=" + paramsAndNot + ", paramsLike=" + paramsLike
        + ", results=" + results + ", htmlContent=" + htmlContent + ", userId=" + userId + "]";
  }

}
